package ms.core.serv.tool;

import java.util.concurrent.TimeUnit;

import feign.Feign;
import feign.Headers;
import feign.Param;
import feign.RequestLine;
import feign.Request.Options;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import ms.core.common.ApiResult;
import ms.core.serv.system.Application;
import ms.core.serv.system.LWGAuthorization.LWGAuth;

public class FeignTool {
	public static ApiResult callSys(String url, String fn, LWGAuth auth, Object body) {
		Call call = Feign.builder()
					.encoder(new JacksonEncoder())											// 编码格式
					.decoder(new JacksonDecoder())											// 解码格式
					.options(new Options(5, TimeUnit.SECONDS, 10, TimeUnit.SECONDS, true))	// 连接超时和读超时
					.target(Call.class, url);
		return call.execute(Application.SERVLET_CORE, fn, auth, body!=null ? body:"{}");
	}

	public static ApiResult callWeb(String url, String fn, LWGAuth auth, Object body) {
		Call call = Feign.builder()
				.encoder(new JacksonEncoder())												// 编码格式
				.decoder(new JacksonDecoder())												// 解码格式
				.options(new Options(5, TimeUnit.SECONDS, 10, TimeUnit.SECONDS, true))		// 连接超时和读超时
				.target(Call.class, url);
		return call.execute(Application.SERVLET_WEB, fn, auth, body!=null ? body:"{}");
	}

	public static ApiResult callUrl(String url, LWGAuth auth, Object body) {
		CallEx call = Feign.builder()
				.encoder(new JacksonEncoder())												// 编码格式
				.decoder(new JacksonDecoder())												// 解码格式
				.options(new Options(5, TimeUnit.SECONDS, 10, TimeUnit.SECONDS, true))		// 连接超时和读超时
				.target(CallEx.class, url);
		return call.execute(auth, body!=null ? body:"{}");
	}

	public interface Call {
	    @Headers({"Content-Type: application/json", "Accept: application/json", "auth: {auth}"})
	    @RequestLine("POST /{apiType}?fn={fn}")
	    ApiResult execute(@Param("apiType") String apiType, @Param("fn") String fn, @Param("auth") LWGAuth auth, Object body);
	}

	public interface CallEx {
		@Headers({"Content-Type: application/json", "Accept: application/json", "auth: {auth}"})
	    @RequestLine("POST")
	    ApiResult execute(@Param("auth") LWGAuth auth, Object body);
	}
}
